package com.wanban.txplayertest

import android.widget.ImageView
import android.widget.ProgressBar
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.wanban.downloader.db.DownloadEntity
import com.wanban.downloader.model.Status
import com.wanban.downloader.utils.CommonUtils

/**
 * Created by fq on 2017/6/7.
 */
class DownloadingAdapter(downloads: ArrayList<DownloadEntity>) :
    BaseQuickAdapter<DownloadEntity, BaseViewHolder>(R.layout.item_downloading, downloads) {

    override fun convert(holder: BaseViewHolder, item: DownloadEntity) {
        // 设置标题
        holder.setText(R.id.tvDlName, item.videoName + "_" + item.episodeName + "_" + item.sourceId)
        // 设置封面
        val imageView = holder.getView<ImageView>(R.id.iv_dl)
        if (imageView.background == null) {
            ImageUtils.loadBannerImage(imageView, R.drawable.hyys_bg_default_h)
        }
        // 设置当前大小
        holder.setText(R.id.tvDlSize, ConvertUtils.byte2FitMemorySize(item.contentLength, 2))
        // 设置下载进度
        val progressBar = holder.getView<ProgressBar>(R.id.progressBar)
        progressBar.progress = item.percent.toInt()
        // 设置状态
        when (item.state) {
            Status.PENDING -> {
                holder.setText(R.id.tvDlSpeed, "等待下载")
                    .setTextColorRes(R.id.tvDlSpeed, R.color.c999999)
                    .setBackgroundResource(R.id.ivDownloadStatus, R.drawable.ic_status_wait)

            }
            Status.START, Status.PREPARE -> {
                holder.setTextColorRes(R.id.tvDlSpeed, R.color.c0A7FF5)
                holder.setBackgroundResource(R.id.ivDownloadStatus, R.drawable.ic_status_downloading)
                holder.setText(R.id.tvDlSpeed, "准备下载")
            }
            Status.DOWNLOADING -> {
                holder.setTextColorRes(R.id.tvDlSpeed, R.color.c0A7FF5)
                holder.setBackgroundResource(R.id.ivDownloadStatus, R.drawable.ic_status_downloading)
                holder.setText(R.id.tvDlSpeed, CommonUtils.getFormatSpeedString(item.speed) + " - " +CommonUtils.getFormatPercentString(item.percent))
            }
            Status.STOP -> {
                holder.setText(R.id.tvDlSpeed, "已暂停")
                    .setTextColorRes(R.id.tvDlSpeed, R.color.c999999)
                    .setBackgroundResource(R.id.ivDownloadStatus, R.drawable.ic_status_pause)
            }
            Status.ERROR -> {
                holder.setText(R.id.tvDlSpeed, item.errorMsg)
                    .setTextColorRes(R.id.tvDlSpeed, R.color.cE04362)
                    .setBackgroundResource(R.id.ivDownloadStatus, R.drawable.ic_status_pause)

            }
            Status.SUCCESS -> {
                holder.setText(R.id.tvDlSpeed, "下载完成")
                    .setTextColorRes(R.id.tvDlSpeed, R.color.purple_500)
                    .setBackgroundResource(R.id.ivDownloadStatus, 0)
            }

            else -> {
                holder.setText(R.id.tvDlSpeed, "闲置状态")
                    .setTextColorRes(R.id.tvDlSpeed, R.color.c999999)
                    .setBackgroundResource(R.id.ivDownloadStatus, 0)
                holder.setText(R.id.tvDlSize, "0MB")
                progressBar.progress = 0
            }
        }


    }



}